<?php
	$servername = "39.104.101.46";
	$username = "root";
	$password = "root";
	$dbname = "parkingData";
	$conn = null;
	try {
    	$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    	$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    	$stmt = $conn->prepare("SET NAMES utf8"); 
    	$stmt->execute();
	}
	catch(PDOException $e)
	{
    	echo $e->getMessage();
	}

	function setData($parkingName,$d){
		global $conn;
		if($d[5]=="null"){
			$stmt = $conn->prepare("INSERT INTO pass_record values('$parkingName','$d[0]','$d[1]','$d[2]','$d[3]','$d[4]',null,$d[6],$d[7],0,NULL) ON DUPLICATE KEY UPDATE pass_type='$d[3]',exit_time='$d[5]',should_fee='$d[6]',practice_fee='$d[7]'"); 
		}
    	else {
    		$stmt = $conn->prepare("INSERT INTO pass_record values('$parkingName','$d[0]','$d[1]','$d[2]','$d[3]','$d[4]','$d[5]',$d[6],$d[7],0,NULL) ON DUPLICATE KEY UPDATE pass_type='$d[3]',exit_time='$d[5]',should_fee='$d[6]',practice_fee='$d[7]'"); 
    	}
    	$stmt->execute();
    	return true;
	}

	function setHeart($parkingName,$heart){
		global $conn;
    	$stmt = $conn->prepare("INSERT INTO parking_heart_log values('$parkingName',now()) ON DUPLICATE KEY UPDATE last_update_time=now()"); 
    	$stmt->execute();
    	
    	$stmt = $conn->prepare("SELECT table_name,query_sql FROM parking_query_sql where parking_software_type='$heart'");
    	$stmt->execute();
    	$row=$stmt->fetchAll();
    	if(empty($row)) return null;
    	else return $row;
	}

	function setError($parkingName,$errorCode,$msg,$isExit){
		global $conn;
    	$stmt = $conn->prepare("INSERT INTO parking_catch_error_log values('$parkingName','$errorCode','$msg','$isExit',now())"); 
    	$stmt->execute();
    	return true;
	}

	function getParkingStat(){
		global $conn;
    	$stmt = $conn->prepare("SELECT parking_name,last_update_time,TIMESTAMPDIFF(SECOND,last_update_time,now()) as last FROM parking_heart_log order by last desc");
    	$stmt->execute();
    	$row=$stmt->fetchAll();
    	if(empty($row)) return null;
    	else return $row;
	}

	function getErrorMsg($parkingName,$start,$end){
		global $conn;
    	$stmt = $conn->prepare("SELECT * FROM parking_catch_error_log where parking_name='$parkingName' order by create_time desc limit $start,$end");
    	$stmt->execute();
    	$row=$stmt->fetchAll();
    	if(empty($row)) return "";
    	else return $row;
	}

	function getErrorMsgCount($parkingName){
		global $conn;
    	$stmt = $conn->prepare("SELECT count(*) as size FROM parking_catch_error_log where parking_name='$parkingName'");
    	$stmt->execute();
    	$row=$stmt->fetchAll();
    	if(empty($row)) return "";
    	else return $row[0][0];
	}

	function getCarPassRecord($parkingName,$start,$end){
		global $conn;
    	$stmt = $conn->prepare("SELECT car_plate,car_type,pass_type,enter_time,exit_time,should_fee,practice_fee FROM pass_record where p_name='$parkingName' order by create_time desc limit $start,$end");
    	$stmt->execute();
    	$row=$stmt->fetchAll();
    	if(empty($row)) return "";
    	else return $row;
	}

	function getCarPassRecordCount($parkingName){
		global $conn;
    	$stmt = $conn->prepare("SELECT count(*) as size FROM pass_record where p_name='$parkingName'");
    	$stmt->execute();
    	$row=$stmt->fetchAll();
    	if(empty($row)) return 0;
    	else return $row[0][0];
	}

	function test(){
		global $conn;
    	$stmt = $conn->prepare("SELECT * FROM parking_heart_log");
    	$stmt->execute();
    	$row=$stmt->fetchAll();
    	if(empty($row)) return null;
    	else return json_encode($row);
	}

	function destory(){
		global $conn;
		$conn->close();
	}

?>

